﻿using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using Common.Unity.Extensions.Entity.Testing.Objects;
using Common.Unity.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Testing.Core;

namespace Common.Unity.Extensions.Entity.Testing
{
	/// <summary>
	/// Summary description for EntityRetrievalPolicyTests
	/// </summary>
	[TestClass]
	[DeploymentItem( "Common.Unity.Extensions.Entity.Testing/Resources/NorthwindEFModel.csdl" )]
	[DeploymentItem( "Common.Unity.Extensions.Entity.Testing/Resources/NorthwindEFModel.msl" )]
	[DeploymentItem( "Common.Unity.Extensions.Entity.Testing/Resources/NorthwindEFModel.ssdl" )]
	public class ServicedEntityCreationPolicyTests : UnityContainerTestBase
	{
		[TestMethod, Owner( TestOwners.MichaelD_Bang )]
		public void VerifyOrdersCorrectlyLoaded()
		{
			const string customerID = "QUICK";
			// NorthwindEntities context = Container.Resolve<NorthwindEntities>();
			var items = Container.Resolve<IEnumerable<Order>>( new { CustomerID = customerID } );
			Assert.AreEqual( 28, items.Count() );
			/*
			foreach ( Order item in items )
			{
				// HACK: Sigh...
				context.LoadProperty( item, "Customer" );
				Assert.IsNotNull( item.Customer );
				Assert.AreEqual( customerID, item.Customer.CustomerID );
			}
			*/
		}

		[TestMethod, Owner( TestOwners.MichaelD_Bang )]
		public void VerifySimpleOrderLoadedCorrectly()
		{
			int orderID = 10259;
			Order item = Container.Resolve<Order>( new { ID = orderID } );
			Assert.IsNotNull( item );
			Assert.IsNull( item.Customer );
			Assert.AreEqual( orderID, item.OrderID );
		}

		[TestMethod, Owner( TestOwners.MichaelD_Bang )]
		public void VerifyOrderWithRelationshipLoadedCorrectly()
		{
			ClearContainer();
			int orderID = 10259;
			Order item = Container.Resolve<Order>( "OrderWithCustomer", new { ID = orderID } );
			Assert.IsNotNull( item );
			Assert.IsNotNull( item.Customer );
			Assert.AreEqual( "CENTC", item.Customer.CustomerID );
			Assert.AreEqual( orderID, item.OrderID );
		}

	}
}
